$(function (){
    $('#top-two').load('/front/top-two.html')
    $('#foot').load('/front/foot.html')


//    获取新闻类型
    $.get({
        url: 'http://localhost:8080/newsType/find',
        dataType: 'json',
        success(res){
            let newsType=$('#newsType')
            if(res.data.length>0){
                newsType.empty();
                for (const datum of res.data) {
                    let div='<div onclick="getNews('+datum.id+')">'+datum.newsTypeName+'</div>'
                    newsType.append(div)
                }
                //    默认获取新闻
                getNews(res.data[0].id)
            }
        }
    })



})


function getNews(val){
    let news=new Object();
    news.newsTypeId=val;
    $.get({
        url:'http://localhost:8080/news/find',
        data: news,
        dataType: 'json',
        success(res) {
            let news=$('#news');
            news.empty();
            let count =0;
            if(res.data.length>0){
                for (const datum of res.data) {
                    count++;
                    let yearMonth=datum.date.substring(0,7);
                    let day=datum.date.substring(8,9);
                    let div='<div class="justify-content-space-between align-items-center box">\n' +
                        '                                    <div class="justify-content-center flex-wrap">\n' +
                        '                                        <div>'+day+'</div>\n' +
                        '                                        <div>'+yearMonth+'</div>\n' +
                        '                                    </div>\n' +
                        '                                    <div onclick="newsShow('+datum.id+')">\n' +
                        '                                        <div>'+datum.title+'</div>\n' +
                        '                                        <div>'+datum.content+'</div>\n' +
                        '                                    </div>\n' +
                        '                                </div>'
                    news.append(div)
                    if(count>=7)
                        break;
                }
            }
        }
    })
}

function newsShow(val){
    sessionStorage.setItem('newsId',val);
    location.href='/front/news-once.html';
}